{% from 'macros.html' import form_field %}
{% extends 'base.html' %}

{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<div class="page-header">
    <br>
    <h1>{{ post.title }}</h1>
    <small>
        分类: <a href="{{ url_for('.show_category',category_id=post.category.id) }}">{{ post.category.name }}</a><br>
        日期: {{ moment(post.timestamp).format('LLL') }}
        <br>
    </small>


</div>
    <br>
    <div class="row">
        <div class="col-sm-8">
            {{ post.body|safe }}
            <hr>
{#            <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="postLinkModal">分享</button>#}
{#            <div class="modal fade postLinkModal" tabindex="-1" role="dialog" aria-hidden="true">#}
{#                <div class="modal-dialog">#}
{#                    <div class="modal-content">#}
{#                        <div class="modal-header">#}
{#                            <h5 class="modal-title">分享链接</h5>#}
{#                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">#}
{#                                <span aria-hidden="true">&times;</span>#}
{#                            </button>#}
{#                        </div>#}
{##}
{#                        <div class="modal-body">#}
{#                            <div class="form-group">#}
{#                                <input type="text" class="form-control" value="{{ url_for('.show_post',slug=post.title,_external=True) }}" readonly>#}
{#                            </div>#}
{#                        </div>#}
{#                    </div>#}
{#                </div>#}
{#            </div>#}
            <br>
            <br>
        </div>


{#        侧边栏#}
        <div class="col-sm-4 sidebar">
            {% include "_sidebar.html" %}
        </div>



{#        回复用户提示框#}
        <br>
        <br>
            {% if request.args.get('reply') %}
        <div class="alert alert-dark col-sm-8">
            回复 <strong>{{ request.args.get('author') }}</strong>
            <a class="float-right" href="{{ url_for('.show_post',slug=post.title) }}">Cancel</a>
        </div>
    {% endif %}


                <div class="col-sm-8 comments" id="comments">

            <h3>{{ comments|length }} 评论
                <small>
                    <a href="{{ url_for('.show_post',slug=post.title,page=pagination.pages or 1) }}#comments">最新的</a>
                </small>
            </h3>
            {% if comments %}
                <ul class="list-group">
                    {% for comment in comments %}
                        <li class="list-group-item list-group-item-action flex-column">
                            <div class="d-flex w-100 justify-content-between">
                                <h5 class="mb-1">
                                    <a href="{% if comment.site %}{{ comment.site }}{% else %}#{% endif %}" target="_blank">
                                        {% if comment.from_admin %}
                                            {{ admin.name }}
                                        {% else %}
                                            {{ comment.author }}
                                        {% endif %}
                                    </a>
                                    {% if comment.from_admin %}
                                        <span class="badge badge-primary">作者</span>
                                    {% endif %}
                                    {% if comment.replied %}
                                        <span class="badge badge-light">回复</span>
                                    {% endif %}
                                </h5>
                                <small data-toggle="tooltip" data-placement="top" data-delag="500" data-timestamp="{{ comment.timestamp.strftime('%Y-%m-%dT%H:%M:%SZ') }}">{{ moment(comment.timestamp).fromNow() }}</small>
                            </div>
                            {% if comment.replied %}
                                <p class="alert alert-dark reply-body">{{ comment.replied.author }}:<br>{{ comment.replied.body }}</p>
                            {% endif %}
                            <p class="mb-1">{{ comment.body }}</p>
                                <div class="float-right">
                                    <a class="btn btn-light btn-sm" href="{{ url_for('.reply_comment',comment_id=comment.id) }}">回复</a>

                                    {% if current_user.is_authenticated %}
                                        <form action="{{ url_for('admin.delete_comment',comment_id=comment.id,next=request.full_path) }}" method="post" class="inline">
                                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure to delete this comment?');">删除</button>
                                        </form>

                                    {% endif %}


                                </div>
                        </li>
                    {% endfor %}
                </ul>
            {% else %}
                <div class="tip"><h5>没有评论</h5></div>
            {% endif %}
                    {% if comments %}
            {% from 'bootstrap/pagination.html' import render_pagination %}
            {{ render_pagination(pagination,fragment='#comments') }}
        {% endif %}


        </div>



    </div>



    <div class="col-sm-8 comment-form form-control">
        <form action="" method="post">
            {{ form.csrf_token }}
            <div class="form-group required">
                {{ form_field(form.author) }}
            </div>
            <div class="form-group required">
                {{ form_field(form.email) }}
            </div>
            <div class="form-group required">
                {{ form_field(form.site) }}
            </div>
            <div class="form-group required">
                {{ form_field(form.body) }}
            </div>
            {{ form.submit(class='btn btn-primary') }}
        </form>
    </div>



{% endblock %}